<script type="text/javascript">
	var arrCoa = $.parseJSON('<?php echo json_encode($arrCoa) ?>');
	printConsole('<?php echo $errorMessage ;?>');
	$(function(){
		$("#tblData").treetable({ expandable: true });
		$("#tblData tbody").on("mousedown", "tr", function() {
			  $(".selected").not(this).removeClass("selected");
			  $(this).toggleClass("selected");
			});

		$("#dialog-form").dialog({
			autoOpen: false,
			height: 330,
			width: 500,
			modal: true,
			/*
			buttons: [
				{
					text:"Simpan",
					icons:{ primary: "ui-icon-check" },
					click:saveCoa,
				},
				{
					text:"Batal",
					icons: { primary: "ui-icon-closethick" },
					click:function(){
						$("#dialog-form").dialog("close");	
					},
				},
			],
			close: function() {
				$("#form")[0].reset();
			}
			*/
		});
	});
	
	function addCoa(index){
		$("#id").val();
		$("#op").val("add");
		$("#parent_acc_code").html(arrCoa[index].acc_code + "." );
		$("#parent_id").val(arrCoa[index].id);
		$("#temp_acc_code").val();
		$("#is_leaf").val(1);
		$("#normal_position").prop('disabled', false);
		$("#normal_position").val(1);
		$("#start_balance").prop('disabled', false);
		$("#start_balance").val(0);
		$("#balance_date").prop('disabled', false);
		$("#balance_date").val("");		
		$("#dialog-form").dialog("open");	
	}

	function saveCoa(){
		//submit form (langsung atau ajax)
		$("#form").submit();
		//$("#dialog-form").dialog("close");	
	}
	
	
	function editCoa(index){
		$("#id").val(arrCoa[index].id);
		$("#op").val("edit");

		var parentAccCode ="";
		if (arrCoa[index].parent_acc_code  != null){
			parentAccCode = arrCoa[index].parent_acc_code + ".";
		}
		
		$("#parent_acc_code").html(parentAccCode);
		var accCode = arrCoa[index].acc_code;
		var tmpAccCode = accCode.substring(parentAccCode.length)
		$("#parent_id").val(arrCoa[index].parent_id);
		$("#tmp_acc_code").val(tmpAccCode);
		$("#acc_name").val(arrCoa[index].name);
		$("#notes").val(arrCoa[index].notes);
		$("#is_leaf").val(arrCoa[index].is_leaf);
		$("#is_leaf").prop('disabled', true);
		if (arrCoa[index].is_leaf == 1){
			$("#normal_position").prop('disabled', false);
			$("#start_balance").prop('disabled', false);
			$("#balance_date").prop('disabled', false);
			$("#normal_position").val(arrCoa[index].normal_position);
			$("#start_balance").val(arrCoa[index].start_balance);
			$("#balance_date").val(arrCoa[index].balance_date);		
		}else{
			$("#normal_position").prop('disabled', true);
			$("#start_balance").prop('disabled', true);
			$("#balance_date").prop('disabled', true);
		}

		if (arrCoa[index].is_active == 1){
			$("#is_active").prop('checked', true);
		}else{
			$("#is_active").prop('checked', false);
		}
		
		$("#dialog-form").dialog("open");
	}
	
	function closeDialog(){
		$("#dialog-form").dialog("close");
	}
	function isLeafChange(){
		isLeaf = $("#is_leaf").val();
		if (isLeaf == 1){
			$("#normal_position").prop('disabled', false);
			$("#start_balance").prop('disabled', false);
			$("#balance_date").prop('disabled', false);
		}else{
			$("#normal_position").prop('disabled', true);
			$("#start_balance").prop('disabled', true);
			$("#balance_date").prop('disabled', true);
		}
	}
</script>


<div style="">

	<div class="notifBox">
		<?php echo $formMessage?>
	</div>
	
	<div style="float:rigth">
	<?php
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					
					array(
						'id' => "btnPrint",
						'title' => Yii::t("menu","Cetak"),
						'icon' => "images/action/Actions-document-print-icon2.png",
						'callback'=>"printData"
					),					
					
				)
			)
		);
	?>
	</div>	
	<div style="clear:both"></div>	
	
	<div>
		<div class="loading ui-state-default ui-state-active" id="load_tblData" style="display: none;position:absolute;top: 150px;left:200px">Loading...</div>	
		<table id="tblData" class="ui-jqgrid-htable">
			<thead style="font-family: 'Lucida Grande', 'Lucida';font-size:12px" >	
			<tr class="ui-jqgrid-labels">
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" >Name</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr">Kode</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr">Notes</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width:40px;">Position</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width:40px;">Balance Date</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width:100px;text-align:right">Balance</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width:40px;text-align:center">Active</th>
				<th role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width:60px;text-align:center">Action</th>				
			</tr>			
			</thead>
			<?php 
				$i =0;
				foreach($arrCoa as $coa){
					echo "<tr data-tt-id='". $coa["id"]. "' data-tt-parent-id='". $coa["parent_id"] ."'>";
					echo "<td>$coa[name]</td>";
					echo "<td>$coa[acc_code]</td>";
					echo "<td>$coa[notes]</td>";
					$pos = $coa["normal_position"] == "D" ? "DEBET" : "CREDIT";
					
					echo "<td>$pos</td>";					
					if ($coa["is_leaf"]){
						echo "<td>".  Utils::formatDate($coa["balance_date"],"Y-m-d","d-M-Y") . "</td>";
						echo "<td style='text-align:right'>". Utils::formatCurrency($coa["start_balance"]) . "</td>";
						
					}else{
						echo "<td></td><td></td>";
						
					}
					
					if($coa["is_active"]){
						echo "<td style='text-align:center'><input type='checkbox' checked disabled/></td>";
					}else{
						echo "<td style='text-align:center'><input type='checkbox' disabled/></td>";
					}
					
					if ($coa["is_leaf"]){
						echo "<td style='text-align:center'><img class='imgBtn' title='Edit' src='images/action/Actions-Edit-icon.png' style='width:20px;height:20px' onclick='editCoa($i)'/></td>";
					}else{
						echo "<td style='text-align:center'>
								<img class='imgBtn' title='Edit' src='images/action/Actions-Edit-icon.png' style='width:20px;height:20px' onclick='editCoa($i)'/>
								<img class='imgBtn' title='Add' src='images/action/Actions-list-add-icon.png' style='width:20px;height:20px' onclick='addCoa($i)'/>
							</td>";
					}
					echo "</tr>";
					$i++;
				}
				
			?>
		</table>
	</div>
	
	<div id="dialog-form" title="Add Transaction Type">
		<form id="form" name="form" action="index.php?r=keuangan/accounting/coa/edit" method="post" >
			<div>
				<input type="text" id="id" name="id" style="width:20px" value=""/>
				<input type="text" id="parent_id" name="parent_id" style="width:20px" value=""/>
				<input type="text" id="op" name="op" style="width:20px" value=""/>
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Kode Akun")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<label id="parent_acc_code" class="form mandatory" ></label>
				<input type="text" id="tmp_acc_code" name="tmp_acc_code" style="width:50px" value=""/>				 				
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Nama")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<input type="text" id="acc_name" name="acc_name" style="width:300px" value=""/>				 				
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Notes")?></label>
				<label class="form mandatory" style="width:10px" >:</label>
				<textarea id="notes" name="notes" style="width:300px;height:50px"></textarea>				 				
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Jenis Akun")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<select id="is_leaf" name="is_leaf" onChange="isLeafChange()">
					<option value="1">Leaf Akun</option>
					<option value="0">Group Akun</option>
				</select>
			</div>
			
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Posisi")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<select id="normal_position" name="normal_position">
					<option value="D">Debet</option>
					<option value="C">Credit</option>
				</select>
			</div>
			
			
			
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Balance")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<input type="text" class="currency" id="start_balance" name="start_balance" style="width:80px;margin-left:2px" value=""/>		
			</div>
			
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Balance Date")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<?php
					$this->widget('application.components.widgets.DateTextField', array('name'=>'balance_date','value'=>date("d-M-Y")));
				?>
			</div>
			<div>
				<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Balance Date")?></label>
				<label class="form mandatory" style="width:10px">:</label>
				<input type="checkbox" name="is_active" id ="is_active" checked/>
			</div>
			
			<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix" style="margin-top:20px">
				<?php 
					$this->widget('application.components.widgets.ActionBar', 
						array("menuAlign" => "right", 
							'items' => array(
								array(
									'id' => "btnRefresh",
									'title' => "Simpan",
									'icon' => "images/action/Actions-document-save-all-icon.png",
									'callback'=>"saveCoa"
								),					
								array(
									'id' => "btnCancel",
									'title' => "Batal",
									'icon' => "images/action/Actions-dialog-cancel-icon.png",
									'callback'=>"closeDialog"
								),
							)
						)
					);
				?>
			</div>
			
		</form>	
	</div>
</div>